/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.eftimoff.empty.slice;

import com.eftimoff.empty.ResourceTable;
import com.eftimoff.ohosplayer.Player;
import com.eftimoff.ohosplayer.actions.arc.CurveAction;
import com.eftimoff.ohosplayer.actions.property.PropertyAction;
import com.eftimoff.ohosplayer.listeners.PlayerEndListener;
import com.eftimoff.ohosplayer.listeners.PlayerStartListener;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

public class TestAbilitySlice extends AbilitySlice {
    private int caseNo;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_test);
        final Component activityMainMessageIcon = findComponentById(ResourceTable.Id_activityMainMessageIcon);
        final Component activityMainMobile = findComponentById(ResourceTable.Id_activityMainMobile);
        final Component activityMainMobileNumber = findComponentById(ResourceTable.Id_activityMainMobileNumber);
        final Component activityMainCallIcon = findComponentById(ResourceTable.Id_activityMainCallIcon);
        final Component activityMainMobileNumberLayout = findComponentById(
            ResourceTable.Id_activityMainMobileNumberLayout);
        final Component activityMainPinkFab = findComponentById(ResourceTable.Id_activityMainPinkFab);
        final Component activityMainProfileName = findComponentById(ResourceTable.Id_activityMainProfileName);
        final Component activityMainheaderLayout = findComponentById(ResourceTable.Id_activityMainheaderLayout);
        caseNo = intent.getIntParam("Case", 0);
        if (caseNo == 1) {
            animateSampleOne(activityMainMobileNumberLayout, activityMainPinkFab, activityMainheaderLayout);
        } else if (caseNo == 2) {
            animateSampleTwo(activityMainMobileNumberLayout, activityMainPinkFab, activityMainheaderLayout);
        } else if (caseNo == 3) {
            animateSampleThree(activityMainProfileName, activityMainMobileNumberLayout, activityMainPinkFab,
                activityMainheaderLayout, activityMainMessageIcon);
        } else if (caseNo == 4) {
            animateSampleFour(activityMainMessageIcon, activityMainMobile, activityMainMobileNumber,
                activityMainCallIcon, activityMainPinkFab, activityMainheaderLayout);
        } else if (caseNo == 5) {
            animateSampleFive(activityMainProfileName);
        }
    }

    private void animateSampleOne(Component activityMainMobileNumberLayout, Component activityMainPinkFab,
        Component activityMainheaderLayout) {
        final PropertyAction fabAction = PropertyAction.newPropertyAction(activityMainPinkFab)
            .scaleX(0)
            .scaleY(0)
            .duration(750)
            .build();
        final PropertyAction headerAction = PropertyAction.newPropertyAction(activityMainheaderLayout)
            .translationY(-100)
            .duration(550)
            .alpha(0.4f)
            .build();
        final PropertyAction bottomAction = PropertyAction.newPropertyAction(activityMainMobileNumberLayout)
            .translationY(900)
            .duration(750)
            .alpha(0f)
            .build();
        Player.init().
            animate(headerAction).
            then().
            animate(fabAction).
            then().
            animate(bottomAction).
            play();
    }

    private void animateSampleTwo(Component activityMainMobileNumberLayout, Component activityMainPinkFab,
        Component activityMainheaderLayout) {
        final PropertyAction fabAction = PropertyAction.newPropertyAction(activityMainPinkFab)
            .translationX(200)
            .duration(750)
            .build();
        final PropertyAction headerAction = PropertyAction.newPropertyAction(activityMainheaderLayout)
            .duration(550)
            .alpha(0.0f)
            .build();
        final PropertyAction bottomAction = PropertyAction.newPropertyAction(activityMainMobileNumberLayout)
            .duration(2500)
            .alpha(0f)
            .build();
        final CurveAction curveAction = CurveAction.newControlPointsCurveAction(activityMainMobileNumberLayout)
            .translationX(-500)
            .translationY(100)
            .controlPoint1X(-500)
            .controlPoint1Y(255.5f)
            .controlPoint2X(0)
            .controlPoint2Y(255.5f)
            .duration(2500)
            .build();
        Player.init().
            animate(headerAction).
            then().
            animate(fabAction).
            then().
            animate(bottomAction).
            animate(curveAction).
            play();
    }

    private void animateSampleThree(Component activityMainProfileName, Component activityMainMobileNumberLayout,
        Component activityMainPinkFab, Component activityMainheaderLayout, Component activityMainMessageIcon) {
        final PropertyAction propertyAction = PropertyAction.newPropertyAction(activityMainheaderLayout)
            .translationX(-200)
            .duration(1500)
            .interpolator(Animator.CurveType.BOUNCE)
            .alpha(0.4f)
            .build();
        final PropertyAction headerPropertyAction = PropertyAction.newPropertyAction(activityMainProfileName)
            .translationY(-200)
            .duration(3750)
            .delay(1233)
            .alpha(0.4f)
            .build();
        final PropertyAction viewSecondAction = PropertyAction.newPropertyAction(activityMainPinkFab)
            .translationY(200)
            .duration(750)
            .alpha(0f)
            .build();
        final PropertyAction bottomPropertyAction = PropertyAction.newPropertyAction(activityMainMobileNumberLayout)
            .rotation(-180)
            .scaleX(0.1f)
            .scaleY(0.1f)
            .translationX(-200)
            .duration(750)
            .build();
        Player.init().
            setPlayerStartListener(playerStartListener).
            setPlayerEndListener(playerEndListener).
            animate(propertyAction).
            animate(headerPropertyAction).
            then().
            animate(viewSecondAction).
            then().
            animate(bottomPropertyAction).
            play();
    }

    private void animateSampleFour(Component activityMainMessageIcon, Component activityMainMobile,
        Component activityMainMobileNumber, Component activityMainCallIcon, Component activityMainPinkFab,
        Component activityMainheaderLayout) {
        final PropertyAction fabAction = PropertyAction.newPropertyAction(activityMainPinkFab)
            .scaleX(0)
            .scaleY(0)
            .duration(750)
            .build();
        final PropertyAction headerAction = PropertyAction.newPropertyAction(activityMainheaderLayout)
            .translationY(-200)
            .duration(750)
            .alpha(0.4f)
            .build();
        final PropertyAction bottomAction = PropertyAction.newPropertyAction(activityMainCallIcon)
            .scaleX(0)
            .scaleY(0)
            .duration(750)
            .alpha(0f)
            .build();
        final PropertyAction activityMainMobileNumberAction = PropertyAction.newPropertyAction(activityMainMobileNumber)
            .scaleX(0)
            .scaleY(0)
            .duration(550)
            .alpha(0f)
            .build();
        final PropertyAction activityMainMobileAction = PropertyAction.newPropertyAction(activityMainMobile)
            .translationY(300)
            .duration(750)
            .alpha(0f)
            .build();
        final PropertyAction activityMainMessageIconAction = PropertyAction.newPropertyAction(activityMainMessageIcon)
            .scaleX(0)
            .scaleY(0)
            .duration(350)
            .alpha(0f)
            .build();
        Player.init().
            animate(headerAction).
            then().
            animate(fabAction).
            then().
            animate(bottomAction).
            then().
            animate(activityMainMobileNumberAction).
            animate(activityMainMobileAction).
            then().
            animate(activityMainMessageIconAction).
            play();
    }

    private void animateSampleFive(Component activityMainProfileName) {
        final PropertyAction activityMainMessageIconAction = PropertyAction.newPropertyAction(activityMainProfileName)
            .scaleX(1)
            .scaleY(1)
            .duration(2000)
            .alpha(0f)
            .build();
        Player.init().
            animate(activityMainMessageIconAction).
            then().
            loop(5).
            play();
    }

    private PlayerStartListener playerStartListener = new PlayerStartListener() {
        @Override
        public void onStart() {
            ToastDialog t = new ToastDialog(TestAbilitySlice.this);
            t.setText("Listener start");
            t.setDuration(100000);
            t.show();
        }
    };

    private PlayerEndListener playerEndListener = new PlayerEndListener() {
        @Override
        public void onEnd() {
            ToastDialog t = new ToastDialog(TestAbilitySlice.this);
            t.setText("Listener end");
            t.setDuration(100000);
            t.show();
        }
    };

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}